package com.bocloud.cmp.driver.convertor.os;

import java.util.List;
import java.util.Map;

import org.openstack4j.model.compute.Address;
import org.openstack4j.model.compute.Addresses;
import org.openstack4j.model.compute.Server;

import com.bocloud.cmp.driver.convertor.BeanConvert;
import com.bocloud.cmp.enums.VMStatus;
import com.bocloud.cmp.model.ServerIpModel;
import com.bocloud.cmp.model.ServerModel;
import com.google.common.collect.Lists;

public class ConvertServer implements BeanConvert<ServerModel, Server> {

    @Override
    public List<ServerModel> convertList(List<? extends Server> servers) {
        List<ServerModel> models = Lists.newArrayList();
        for (Server server : servers) {
            models.add(convertModel(server));
        }
        return models;
    }

    @Override
    public ServerModel convertModel(Server server) {
        if (null == server) {
            return null;
        }
        ServerModel model = new ServerModel();
        model.setCpu(server.getFlavor().getVcpus());
        model.setDisk(server.getFlavor().getDisk());
        model.setGmtCreate(server.getCreated());
        model.setImageId(server.getImageId());
        model.setInstanceId(server.getId());
        model.setMemory(server.getFlavor().getRam());
        model.setName(server.getName());
        model.setPassword(server.getAdminPass());
        model.setSecurityGroupId(null);
        model.setManagerIp(server.getAccessIPv4());
        model.setFlavorId(server.getFlavorId());
        model.setStatus(convert(server.getStatus().name()));
        if (null != server.getAddresses()) {
            model.setServerIps(convertIp(server.getAddresses(), server.getId()));
        }
        if (null != server.getHypervisorHostname()) {
            model.setHostName(server.getHypervisorHostname());
        } else {
            model.setHostName(server.getHost());
        }

        return model;
    }

    /**
     * ��ȡ������� ���� ����IP��ַ
     * 
     * @param addresses
     * @param uuid
     * @return
     */
    private List<ServerIpModel> convertIp(Addresses addresses, String uuid) {
        Map<String, List<? extends Address>> addressesMap = addresses.getAddresses();
        List<ServerIpModel> serverIpList = Lists.newArrayList();
        for (Map.Entry<String, List<? extends Address>> set : addressesMap.entrySet()) {
            List<? extends Address> addressList = set.getValue();
            for (Address address : addressList) {
                ServerIpModel serverIpModel = new ServerIpModel();
                serverIpModel.setAddress(address.getAddr());
                serverIpModel.setMac(address.getMacAddr());
                serverIpModel.setType(address.getType());
                serverIpModel.setName(set.getKey());
                if (4 == address.getVersion()) {
                    serverIpModel.setVersion("IPv4");
                } else if (6 == address.getVersion()) {
                    serverIpModel.setVersion("IPv6");
                } else {
                    serverIpModel.setVersion("not support");
                }
                serverIpModel.setUuid(uuid);
                serverIpList.add(serverIpModel);
            }
        }
        return serverIpList;
    }

    private String convert(String status) {
        if (status.equalsIgnoreCase("active")) {
            return VMStatus.RUNNING.name();
            // vm.setStatus(VMStatus.RUNNING.name());
        } else if (status.equalsIgnoreCase("stopped") || status.equalsIgnoreCase("shutoff")) {
            return VMStatus.STOPPED.name();
        } else if (status.equalsIgnoreCase("BUILD")) {
            return VMStatus.BUILDING.name();
        } else if (status.equalsIgnoreCase(VMStatus.SUSPENDED.name())) {
            return VMStatus.SUSPENDED.name();
        } else if (status.equalsIgnoreCase(VMStatus.PAUSED.name())) {
            return VMStatus.PAUSED.name();
        } else {
            return VMStatus.EXCEPTION.name();
        }
    }

}